<?php
/**
 * Created by IntelliJ IDEA.
 * User: fang.cai.liang@aliyun.com
 * Date: 2017/7/3
 * Time: 15:02
 */

namespace nflow;


/**
 * Class AOperator
 * @package nflow
 */
Abstract class AOperator
{

    protected $codeMap = [
        1000 => '请求正常响应!',
        1001 => '认证失败!',
        1002 => '参数错误!',
        1003 => '请求已过期!',
        1004 => '请求重复!',
        1005 => '订单已经存在!',
        1006 => '余额不足!',
        1007 => '充值失败!'
    ];
    
    /**
     * 获取 产品列表
     * @param $phone
     * @return mixed
     */
    public abstract function getProductList($phone);

    /**
     * 下单 接口
     * @param $phone
     * @param $pid
     * @param $dorderId
     * @param $efftype
     * @return mixed
     */
    public abstract function order($phone, $pid, $dorderId, $efftype);

    /**
     * 订单状态 查询
     * @param $phone
     * @param $pid
     * @param $dorderId
     * @return mixed
     */
    public abstract function qorder($phone, $pid, $dorderId);

    /**
     * 构建统一的返回结果
     * @param $curlResult
     * @param $method
     * @return mixed
     */
    public abstract function buildResult($curlResult, $method);

    /**
     * 发送 http 请求, 默认为 post 方式
     * @param $url
     * @param $data
     * @param $header
     * @param int $post
     * @return mixed
     * @throws \Exception
     */
    protected function http($url, $data, $header, $post = 1)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POST, $post);
        if($post)
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        $result = curl_exec($ch);
        if($result == false) {
            $msg = curl_error($ch);
            curl_close($ch);
            throw new \Exception($msg, 10002);
        }
        curl_close($ch);
        return $result;
    }
}